package com.es.spring_es.services;

import com.es.spring_es.clientmap.EsClientMap;
import com.es.spring_es.clientmap.EsUtils;
import com.es.spring_es.model.DriverLocationEntity;
import com.es.spring_es.model.DriverLocationMainEntity;
import org.frameworkset.elasticsearch.ElasticSearchException;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xu
 * @date 2020/6/3
 * @desciption 司机轨迹主类信息
 */
@Service
public class DriverlocationMainServices {

    @Autowired
    @Qualifier("bbossESStarterDefault")
    private BBossESStarter bbossESStarterDefault;
   /* @Autowired
    @Qualifier("bbossESStarterLogs")
    private BBossESStarter bbossESStarterLogs;*/

    @Autowired
    EsClientMap esClientMap;

    @Autowired
    EsUtils esUtils;

    /**
     * 创建索引表
     */
    public void CreateDriverLocationMainIndice(){
        //创建加载配置文件的客户端工具，单实例多线程安全
        ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient("esmapper/driver_location_main.xml");

        try {
          //  esUtils.CreateDriverLocationMainIndice("driver_location_main","createDriverLocationMainIndice");
            //判读索引表demo是否存在，存在返回true，不存在返回false
            boolean exist = clientUtil.existIndice("driver_location_main");

            //如果索引表demo已经存在先删除mapping
            if(exist) {
              /*  String r = clientUtil.dropIndice("driver_location_main");
                System.out.println(r);
                exist = clientUtil.existIndice("driver_location_main");
//				r = clientUtil.dropIndice("demo");
//				System.out.println(r);
                String demoIndice = clientUtil.getIndice("driver_location_main");//获取最新建立的索引表结构
                System.out.println(demoIndice);*/
            }else {
                //创建索引表demo
                clientUtil.createIndiceMapping("driver_location_main",//索引表名称
                        "createDriverLocationMainIndice");//索引表mapping dsl脚本名称，在esmapper/demo7.xml中定义createDemoIndice
            }

            String demoIndice = clientUtil.getIndice("driver_location_main");//获取最新建立的索引表结构
            System.out.println(demoIndice);
        } catch (ElasticSearchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 增加数据
     */
    public void addDriverLocationMainData(DriverLocationMainEntity driverLocationMainEntity){
        try {
            //String driver_location_main = clientUtil.addDocument("driver_location_main", driverLocationMainEntity,"refresh");
            String driver_location_main = esUtils.addOrUpdateDocument("driver_location_main", driverLocationMainEntity);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询司机位置
     */
    public List<DriverLocationMainEntity> getDriverLocationMainByTrid(String trid ){
        try {
           // ESDatas<DriverLocationMainEntity> driver_location1 = clientUtil.searchAll("driver_location_main", DriverLocationMainEntity.class);
           /* List<DriverLocationMainEntity> driver_location_main = esUtils.getDocumentDataAll("driver_location_main", DriverLocationMainEntity.class);
            System.out.println(driver_location_main.toString());*/
            Map<String,Object> params = new HashMap<String,Object>();
            //设置applicationName1和applicationName2两个变量的值
            params.put("trid",trid);


            List<DriverLocationMainEntity> exec = esUtils.exec("driver_location_main", params, "searchDriverLocationMainByTrid", DriverLocationMainEntity.class);
            System.out.println(exec.toString());
            return exec;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
